package net.zjitc.controller.admin;

import com.github.pagehelper.PageInfo;
import net.zjitc.common.ResultMessage;
import net.zjitc.pojo.Notice;
import net.zjitc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @AUTHOR zhj
 * @DATE 2019/3/28 14:48
 */
@Controller
public class NoticeController {
    @Autowired
    NoticeService noticeService;
    @RequestMapping("/manage/addNotice")
    @ResponseBody
    public ResultMessage addNotice(@RequestBody Notice notice){
        int resultCount = noticeService.addNotice(notice);
        if (resultCount>0){
             return ResultMessage.createSuccessMessage("添加成功");
        }
        return ResultMessage.createErrorMessage("添加失败");
    }



    @RequestMapping("/manage/findAll")
    public String findAll(Notice notice, Model model){
        List<Notice> list = noticeService.findAll();
        model.addAttribute("list",list);
        return "/admin/notices/list.jsp";
    }


    /**
     * 修改页面的数据回显
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/manage/editUI")
    public String findById(int id, Model model){
        Notice notice= noticeService.findById(id);
        model.addAttribute("notice",notice);
        return "/admin/notices/edit.jsp";
    }


    @RequestMapping("/manage/editNotice")
    @ResponseBody
    public ResultMessage editNotice(@RequestBody Notice notice, Model model){
        int resultCount = noticeService.editNotice(notice);
        if (resultCount>0){
            return ResultMessage.createSuccessMessage("修改成功");
        }
        return ResultMessage.createErrorMessage("修改失败");

    }

    @RequestMapping("/manage/deleteNotice")
    @ResponseBody
    public ResultMessage deleteNotice(int id, Model model){
        int resultCount = noticeService.deleteNotice(id);
        if (resultCount>0){
            return ResultMessage.createSuccessMessage("删除成功");
        }
        return ResultMessage.createErrorMessage("删除失败");

    }

    @RequestMapping("/manage/findByPage")
    public String findByPage(Integer currentpage, @RequestParam(required = false) Integer pageSize, Model model){
        PageInfo<Notice> pageInfo = noticeService.findNoticeByPage(currentpage, pageSize);
        model.addAttribute("p",pageInfo);
        return "/admin/notices/list.jsp";
    }

}
